<!--
 * @Description: 确认订单页面组件
 * @Author: hai-27
 * @Date: 2020-02-23 23:46:39
 * @LastEditors: hai-27
 * @LastEditTime: 2020-03-29 13:10:21
 -->
<template>
  <div class="confirmOrder">
    <!-- 头部 -->
    <div class="confirmOrder-header">
      <div class="header-content">
        <p><i class="el-icon-s-order"></i></p>
        <p>确认订单</p>
        <router-link to></router-link>
      </div>
    </div>
    <!-- 头部END -->

    <!-- 主要内容容器 -->
    <div class="content">

      <!-- 选择地址 -->
      <div class="section-address">
        <p class="title">收货地址</p>
        <div class="address-body">
          <ul>
            <li v-for="item in address" :key="item.id" :class="item.is_default ? 'in-section' : ''"
                @click="changeStatus($event, item.id)">
              <!-- 弹框部分 -->
              <el-popover placement="top" style="float:right;">
                <p>确定删除吗？</p>
                <div style="text-align: right; margin: 10px 0 0">
                  <el-button type="primary" size="mini" @click="deleteAddr(item.id)">确定</el-button>
                </div>
                <!-- 删除按钮 × -->
                <i class="el-icon-close delete" slot="reference" v-show="isDelete"></i>
              </el-popover>
              <h2>{{ item.receiver }}</h2>
              <p class="phone">{{ item.receiver_mobile }}</p>
              <p class="address">{{ item.receiver_addr }}</p>
            </li>

            <li class="add-address">
              <i class="el-icon-circle-plus-outline" @click="addAddr"></i>
              <p>添加新地址</p>
            </li>

          </ul>
        </div>
      </div>
      <!-- 选择地址END -->

      <!-- 支付方式 将label的值绑定给radio-->
      <el-radio-group v-model="radio">
        <el-radio class="zhifubao" label="1">
          <i class="iconfont icon-zhifubao" style="font-size: 20px;margin-right: 5px"></i>
          <span :style="{color: '#00A0E9', fontSize: '18px'}">支付宝</span>
        </el-radio>
        <el-radio class="yinlian" label="2">
          <i class="iconfont icon-zhifu-yinlian" style="font-size: 26px;margin-right: 5px;line-height: 30px"></i>
          <span :style="{color: '#00A0E9', fontSize: '18px'}">银联卡</span>
        </el-radio>
      </el-radio-group>

      <!-- 商品及优惠券 -->
      <div class="section-goods">
        <p class="title">商品及优惠券</p>
        <div class="goods-list">
          <ul>
            <li v-for="item in getCheckGoods" :key="item.productID">
              <img :src="item.productImg"/>
              <span class="pro-name">{{ item.productName }}</span>
              <span class="pro-price">{{ item.price }}元 x {{ item.num }}</span>
              <span class="pro-status"></span>
              <span class="pro-total">{{ item.price * item.num }}元</span>
            </li>
          </ul>
        </div>
      </div>
      <!-- 商品及优惠券END -->

      <!-- 配送方式 -->
      <div class="section-shipment">
        <p class="title">配送方式</p>
        <p class="shipment">包邮</p>
      </div>
      <!-- 配送方式END -->

      <!-- 发票 -->
      <div class="section-invoice">
        <p class="title">发票</p>
        <p class="invoice">电子发票</p>
        <p class="invoice">个人</p>
        <p class="invoice">商品明细</p>
      </div>
      <!-- 发票END -->

      <!-- 结算列表 -->
      <div class="section-count">
        <div class="money-box">
          <ul>
            <li>
              <span class="title">商品件数：</span>
              <span class="value">{{ getCheckNum }}件</span>
            </li>
            <li>
              <span class="title">商品总价：</span>
              <span class="value">{{ getTotalPrice }}元</span>
            </li>
            <li>
              <span class="title">活动优惠：</span>
              <span class="value">-0元</span>
            </li>
            <li>
              <span class="title">优惠券抵扣：</span>
              <span class="value">-0元</span>
            </li>
            <li>
              <span class="title">运费：</span>
              <span class="value">0元</span>
            </li>
            <li class="total">
              <span class="title">应付总额：</span>
              <span class="value">
                <span class="total-price">{{ getTotalPrice }}</span>元
              </span>
            </li>
          </ul>
        </div>
      </div>
      <!-- 结算列表END -->
      <AddMyAddr :getStudentName="getStudentName"></AddMyAddr>
      <!-- 结算导航 -->
      <div class="section-bar">
        <div class="btn">
          <router-link to="/shoppingCart" class="btn-base btn-return">返回购物车</router-link>
          <a href="javascript:void(0);" @click="addOrder" class="btn-base btn-primary">结算</a>
        </div>
      </div>
      <!-- 结算导航END -->
    </div>
    <!-- 主要内容容器END -->
  </div>
</template>


<script>
import {mapGetters} from "vuex";
import {mapActions} from "vuex";
import AddMyAddr from '../components/AddMyAddr.vue'

export default {
  name: "",
  data() {
    return {
      isDelete: true, //控制 ❌ 的显示
      radio: "1",  //默认选中1

      // 虚拟数据
      confirmAddress: 1,
      // 地址列表
      address: [
        {
          id: 1,
          receiver: "陈同学",
          receiver_mobile: "13580018623",
          receiver_addr: "广东 广州市 白云区 江高镇 广东白云学院",
          is_default: true
        },
        {
          id: 2,
          receiver: "王同学",
          receiver_mobile: "13512345678",
          receiver_addr: "广东 广州市 白云区 江高镇 广东白云学院",
          is_default: false
        },
      ],
    };
  },
  components: {AddMyAddr,},

  // DOM节点创建完成
  created() {
    // 未勾选购物车商品时，直接进入确认订单页面,提示信息并返回购物车
    if (this.getCheckNum < 1) {
      this.notifyError("请勾选商品后再结算");
      this.$router.push({path: "/shoppingCart"});
    }
    // TODO 获取用户地址信息

  },
  computed: {
    // 结算的商品数量; 结算商品总计; 结算商品信息
    ...mapGetters(["getCheckNum", "getTotalPrice", "getCheckGoods"]) // VUEX 的获取器
  },
  watch: {
    ...mapGetters(["getAddress"])
  },

  // DOM节点完成挂载到页面时，触发
  mounted() {
    // 通过全局事件总线，绑定事件
    this.$bus.$on("test", (data) => {
      // 判断地址 是否为默认地址
      if (data.is_default) {
        //当前地址为默认地址，需将其他地址改为非默认
        for (let i = 0; i < this.address.length; i++) {
          const temp = this.address[i]
          if (temp.is_default) {
            temp.is_default = false
            // this.address[i] = temp
          }
        }
        // 最后将新添加的地址放入address
        this.address.push(data)
      } else {
        // 若添加的地址不是默认地址，就直接放入address即可
        this.address.push(data)
      }
    })
    this.$axios.get('/users/user/userInfo/',{
      headers:{
        Authorization:localStorage.token || ''
      }
    })
    .then(res => {
      console.log('地址返回的响应',res)
      this.address = res.data.addrs.userInfo.addrs
    })
    .catch(err => {
      return Promise.reject(err)
    })
    
  },
  beforeDestroy() {
    this.$bus.$off("test")
  },
  methods: {
    getStudentName(dada){
      console.log('在这里看到了吗',dada)
      this.address.push(dada)
    },
    ...mapActions(["deleteShoppingCart"]),

    // 添加地址
    addAddr() {
      console.log("正添加地址...")
      this.$store.dispatch("setAddMyAddr", true)
      
    },
    // 删除地址
    deleteAddr(id) {
      console.log("删除地址ID:", id)
      //TODO 删除地址
      this.$axios.delete('/user/addAddr/',{
        data:{addr_id:id},
        headers:{
          Authorization: localStorage.token || ''
        }
      })
      .then(res => {
        console.log('删除成功的响应',res)
        // 待实现
        if(res.data.code == 200){
          // 删除前端addrs中的地址
          for(let i=0 ; i<this.address.length;i++){
            let temp = this.address[i]
            if(temp.id == id){
              this.address.splice(i,1)
            }
          }
          this.notifySucceed(res.data.msg)
        }else{
          this.notifyError
        }

      })
      .catch(err => {
        console.log('删除失败的响应',err)
        return Promise.reject(err)
      })


    },

    // ===========切换地址的选中==========
    changeStatus(e, id) {
      for (let i = 0; i < this.address.length; i++) {
        let temp = this.address[i]
        if (temp.id == id) {
          temp.is_default = true
        } else {
          temp.is_default = false
        }
      }
    },


    // 获取默认的地址id
    getSelectedAddr() {
      for (let i = 0; i < this.address.length; i++) {
        if (this.address[i].is_default) {
          return this.address[i].id
        }
      }
    },


    // 创建订单
    addOrder() {
      // TODO 创建订单
      this.$axios.post('/orders/user/order/',{
        products:this.getCheckGoods,
        addr:this.getSelectedAddr(),
        payMethod: this.radio
      },{
        headers:{
          Authorization: localStorage.token || ''
        }
      })
      .then(res => {
        console.log('创建订单的响应',res)
        switch(res.data.code){
          case 200: this.notifySucceed(res.data.msg); this.$router.push({path:'/order'});break;
          case 201: window.location.href=res.data.pay_url;break;
          default : this.notifyError(res.data.msg)
        }
      })
      .catch(err => {
        console.log(err)
        return Promise.reject(err)
      })
      



    }
  },
  // mounted(){
  //   

  // }
};
</script>

<style scoped>
.zhifubao {
  margin: 20px 0;
  margin-left: 50px;
}

.yinlian {
  margin: 20px 0;
  margin-left: 20px;
}

.confirmOrder {
  background-color: #f5f5f5;
  padding-bottom: 20px;
}

/* 头部CSS */
.confirmOrder .confirmOrder-header {
  background-color: #fff;
  border-bottom: 2px solid #ff6700;
  margin-bottom: 20px;
}

.confirmOrder .confirmOrder-header .header-content {
  width: 1225px;
  margin: 0 auto;
  height: 80px;
}

.confirmOrder .confirmOrder-header .header-content p {
  float: left;
  font-size: 28px;
  line-height: 80px;
  color: #424242;
  margin-right: 20px;
}

.confirmOrder .confirmOrder-header .header-content p i {
  font-size: 45px;
  color: #ff6700;
  line-height: 80px;
}

/* 头部CSS END */

/* 主要内容容器CSS */
.confirmOrder .content {
  width: 1225px;
  margin: 0 auto;
  padding: 48px 0 0;
  background-color: #fff;
}

/* 选择地址CSS */
.confirmOrder .content .section-address {
  margin: 0 48px;
  overflow: hidden;
}

.confirmOrder .content .section-address .title {
  color: #333;
  font-size: 18px;
  line-height: 20px;
  margin-bottom: 20px;
}

.confirmOrder .content .address-body li {
  float: left;
  color: #333;
  width: 220px;
  height: 178px;
  border: 1px solid #e0e0e0;
  padding: 15px 24px 0;
  margin-right: 17px;
  margin-bottom: 24px;
}

.confirmOrder .content .address-body .in-section {
  border: 1px solid #ff6700;
}

.confirmOrder .content .address-body li h2 {
  font-size: 18px;
  font-weight: normal;
  line-height: 30px;
  margin-bottom: 10px;
}

.confirmOrder .content .address-body li p {
  font-size: 14px;
  color: #757575;
}

.confirmOrder .content .address-body li .address {
  padding: 10px 0;
  max-width: 180px;
  max-height: 88px;
  line-height: 22px;
  overflow: hidden;
}

.confirmOrder .content .address-body .add-address {
  text-align: center;
  line-height: 30px;
}

.confirmOrder .content .address-body .add-address i {
  font-size: 30px;
  padding-top: 50px;
  text-align: center;
}

/* 选择地址CSS END */

/* 商品及优惠券CSS */
.confirmOrder .content .section-goods {
  margin: 0 48px;
}

.confirmOrder .content .section-goods p.title {
  color: #333;
  font-size: 18px;
  line-height: 40px;
}

.confirmOrder .content .section-goods .goods-list {
  padding: 5px 0;
  border-top: 1px solid #e0e0e0;
  border-bottom: 1px solid #e0e0e0;
}

.confirmOrder .content .section-goods .goods-list li {
  padding: 10px 0;
  color: #424242;
  overflow: hidden;
}

.confirmOrder .content .section-goods .goods-list li img {
  float: left;
  width: 30px;
  height: 30px;
  margin-right: 10px;
}

.confirmOrder .content .section-goods .goods-list li .pro-name {
  float: left;
  width: 650px;
  line-height: 30px;
}

.confirmOrder .content .section-goods .goods-list li .pro-price {
  float: left;
  width: 150px;
  text-align: center;
  line-height: 30px;
}

.confirmOrder .content .section-goods .goods-list li .pro-status {
  float: left;
  width: 99px;
  height: 30px;
  text-align: center;
  line-height: 30px;
}

.confirmOrder .content .section-goods .goods-list li .pro-total {
  float: left;
  width: 190px;
  text-align: center;
  color: #ff6700;
  line-height: 30px;
}

/* 商品及优惠券CSS END */

/* 配送方式CSS */
.confirmOrder .content .section-shipment {
  margin: 0 48px;
  padding: 25px 0;
  border-bottom: 1px solid #e0e0e0;
  overflow: hidden;
}

.confirmOrder .content .section-shipment .title {
  float: left;
  width: 150px;
  color: #333;
  font-size: 18px;
  line-height: 38px;
}

.confirmOrder .content .section-shipment .shipment {
  float: left;
  line-height: 38px;
  font-size: 14px;
  color: #ff6700;
}

/* 配送方式CSS END */

/* 发票CSS */
.confirmOrder .content .section-invoice {
  margin: 0 48px;
  padding: 25px 0;
  border-bottom: 1px solid #e0e0e0;
  overflow: hidden;
}

.confirmOrder .content .section-invoice .title {
  float: left;
  width: 150px;
  color: #333;
  font-size: 18px;
  line-height: 38px;
}

.confirmOrder .content .section-invoice .invoice {
  float: left;
  line-height: 38px;
  font-size: 14px;
  margin-right: 20px;
  color: #ff6700;
}

/* 发票CSS END */

/* 结算列表CSS */
.confirmOrder .content .section-count {
  margin: 0 48px;
  padding: 20px 0;
  overflow: hidden;
}

.confirmOrder .content .section-count .money-box {
  float: right;
  text-align: right;
}

.confirmOrder .content .section-count .money-box .title {
  float: left;
  width: 126px;
  height: 30px;
  display: block;
  line-height: 30px;
  color: #757575;
}

.confirmOrder .content .section-count .money-box .value {
  float: left;
  min-width: 105px;
  height: 30px;
  display: block;
  line-height: 30px;
  color: #ff6700;
}

.confirmOrder .content .section-count .money-box .total .title {
  padding-top: 15px;
}

.confirmOrder .content .section-count .money-box .total .value {
  padding-top: 10px;
}

.confirmOrder .content .section-count .money-box .total-price {
  font-size: 30px;
}

/* 结算列表CSS END */

/* 结算导航CSS */
.confirmOrder .content .section-bar {
  padding: 20px 48px;
  border-top: 2px solid #f5f5f5;
  overflow: hidden;
}

.confirmOrder .content .section-bar .btn {
  float: right;
}

.confirmOrder .content .section-bar .btn .btn-base {
  float: left;
  margin-left: 30px;
  width: 158px;
  height: 38px;
  border: 1px solid #b0b0b0;
  font-size: 14px;
  line-height: 38px;
  text-align: center;
}

.confirmOrder .content .section-bar .btn .btn-return {
  color: rgba(0, 0, 0, 0.27);
  border-color: rgba(0, 0, 0, 0.27);
}

.confirmOrder .content .section-bar .btn .btn-primary {
  background: #ff6700;
  border-color: #ff6700;
  color: #fff;
}

/* 结算导航CSS */

/* 主要内容容器CSS END */
</style>
